<?php

$m = new MongoClient('mongodb://ftp.giemiebier.nl');

$db = $m->chatdb;
$chat = $db->chat;
//echo $pagetree[2];
$cursor = $chat->findOne(["_id" => new MongoId($pagetree[2])]);
foreach ($cursor['ids'] as $user_id) {
    $user_chats = $db->user_chats;
    $user_chats->update(
            array('_id' => $user_id), array(
                '$pull' => array('chat_id' => $pagetree[2])
            )
    );
}
$chat->remove(["_id" => new MongoId($pagetree[2])], array("justOne" => true));